package com.jblog.controller.front.action;

import static com.jblog.Constants.Template.TPLS_FRONT;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;

import com.jblog.common.util.Captcha;
import com.jblog.common.util.MD5;
import com.jblog.common.web.FrontUtils;
import com.jblog.core.model.blog.Post;
import com.jblog.core.model.blog.Tag;
import com.jblog.core.service.facde.AccountFacade;
import com.jblog.core.service.facde.BlogFacade;
import com.jblog.core.service.facde.SiteFacade;
import com.jblog.core.service.mail.MailService;

public class BaseAct {
    @Autowired protected AccountFacade iAccount;
    @Autowired protected BlogFacade iblog;
    @Autowired protected SiteFacade isite;
    @Autowired protected MailService imail;
    
    protected String toKeywords(Post post) {
        List<Tag> tags = iblog.findTagByPostId(post.getId());
        StringBuilder sb = new StringBuilder();
        for (int i = 0, len = tags.size(); i < len; i ++) {
            sb.append(tags.get(i).getName());
            if (i < len - 1) {
                sb.append(",");
            }
        }
        return sb.toString();
    }
    
    protected boolean captchaValidation(HttpServletRequest request, String captcha) {
        if (StringUtils.isNotBlank(captcha)) {
            String old = (String)request.getSession().getAttribute(Captcha.CAPTCHA);
            return old.equals(MD5.twice(captcha));
        }
        return false;
    }
    
	private void setFrontData(HttpServletRequest request, ModelMap model) {
		FrontUtils.frontData(request, model);
		FrontUtils.frontPageData(request, model);
	}
    
    private String F(String name) {
        return TPLS_FRONT + name;
    }
    
    protected String F(HttpServletRequest request, ModelMap model, String name) {
    	setFrontData(request, model);
        return F(name);
    }
    
    protected String O(HttpServletRequest request, ModelMap model, String name) {
	    setFrontData(request, model);
	    return name;
    }
}
